package com.horoscope.astrology.zodiac.signs.free.utils.dialog

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import com.zyyoona7.wheel.WheelView
import com.horoscope.astrology.zodiac.signs.free.R
import com.horoscope.astrology.zodiac.signs.free.utils.SysUtils
import kotlinx.android.synthetic.main.dialog_born.*

class DialogSelectBornTime(private val listener: View.OnClickListener) : BaseDialogFragment() {


    private lateinit var hour: WheelView<String>
    private lateinit var min: WheelView<String>
    private lateinit var wheelView: WheelView<String>
    private lateinit var check: ImageView

    private var hourS = ""
    private var minS = ""

    private val hourList = ArrayList<String>()
    private val minList = ArrayList<String>()
    private val timeList = ArrayList<String>()

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        initData()
        initView(view)
        initListeners()

    }

    private fun initListeners() {
        check.setOnClickListener {
            if (check.tag == 0) {
                check.setImageResource(R.mipmap.dialog_check)
                check.tag = 1
            } else {
                check.setImageResource(R.mipmap.dialog_un_check)
                check.tag = 0
            }
        }
        text_view_ok?.setOnClickListener(listener)
    }

    private fun initView(view: View) {
        check = view.findViewById(R.id.image_view_check)
        hour = view.findViewById(R.id.wheel_view_hour)
        min = view.findViewById(R.id.wheel_view_min)
        wheelView = view.findViewById(R.id.wheel_view_am)

        setWheelView(hour, hourList)
        hour.textAlign = WheelView.TEXT_ALIGN_CENTER
        if (hourS.isEmpty()) {
            hour.selectedItemPosition = SysUtils.getCurrentHour().toInt() - 1
        } else {
            hour.selectedItemPosition = hourS.toInt() - 1
        }

        setWheelView(min, minList)
        min.textAlign = WheelView.TEXT_ALIGN_CENTER
        min.textBoundaryMargin = SysUtils.rp(45f).toFloat()
        if (minS.isEmpty()) {
            min.selectedItemPosition = SysUtils.getCurrentMin().toInt()
        } else {
            min.selectedItemPosition = minS.toInt()
        }

        setWheelView(wheelView, timeList)
        wheelView.textAlign = WheelView.TEXT_ALIGN_CENTER
        wheelView.selectedItemPosition = SysUtils.getAMOrPM()
        check.tag = 0
    }

    private fun initData() {
        hourS = arguments?.getString("time_h") ?: ""
        minS = arguments?.getString("time_min") ?: ""

        for (i in 1..12) {
            hourList.add("$i")
        }
        for (i in 0..59) {
            if (i < 10) {
                minList.add("0$i")
            } else {
                minList.add("$i")
            }
        }

        timeList.add("AM")
        timeList.add("PM")
    }

    fun getTime(): String {
        if (check.tag == 1) {
            return getString(R.string.born_time_do_not_know)
        }
        return hour.selectedItemData + ":" + min.selectedItemData + wheelView.selectedItemData
    }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        return inflater.inflate(R.layout.dialog_born, container, false)
    }

    companion object {
        fun getGenerateDialog(
            listener: View.OnClickListener,
            h: String,
            min: String
        ): DialogSelectBornTime {
            val dialog = DialogSelectBornTime(listener)
            val bundle = Bundle()
            bundle.putString("time_h", h)
            bundle.putString("time_min", min)
            dialog.arguments = bundle
            return dialog
        }
    }

}